<template>
  <view class="content" :style="{ background: `${data[0].pageColor}` }">
    <block v-if="!loadingPage">
      <!-- #ifdef MP-WEIXIN -->
      <view class="" style="height: 100rpx"> </view>
      <!-- #endif -->
      <x-skeleton type="banner" :loading="true">
        <view>我是轮播图</view>
      </x-skeleton>
      <x-skeleton type="menu" :loading="true">
        <view>我是轮播图</view>
      </x-skeleton>
      <x-skeleton type="list" :loading="true">
        <view>我是轮播图</view>
      </x-skeleton>
      <x-skeleton
        type="waterfall"
        v-for="(item, index, key) in 100"
        :key="'waterfall' + index"
        :loading="true"
      >
        <view>我是轮播图</view>
      </x-skeleton>
    </block>

    <block>
      <view v-for="(item, index, key) in data" :key="key">
        <navTitleCom
          v-if="item.name == 'page' && item.type !== 4"
          :info="item"
        />
        <!-- #ifdef MP-WEIXIN -->
        <view
          :style="{ height: `${menuInfo.height}px` }"
          v-if="item.name == 'page' && item.type == 4"
        ></view>
        <!-- #endif -->
        <searchCom v-if="item.name == '搜索'" :info="item" />
        <imgCom v-if="item.name == '图片'" :info="item" />
        <bannerCon v-if="item.name == '轮播图'" :info="item" />
        <imgTextNavCom v-if="item.name == '图文导航'" :info="item" />
        <videoCom v-if="item.name == '视频组件'" :info="item" />
        <tabsCom v-if="item.name == '选项卡'" :info="item" />
        <goodsCateCom v-if="item.name == '商品分组'" :info="item" />
        <titleCom v-if="item.name == '标题栏'" :info="item" />
        <wangeditorCom v-if="item.name == '富文本'" :info="item" />
        <textCom v-if="item.name == '文本'" :info="item" />
        <borderCom v-if="item.name == '分割占位'" :info="item" />
        <sellerCom v-if="item.name == '店铺街'" :info="item" />
        <bulletinCom v-if="item.name == '平台公告'" :info="item" />
        <couponCom v-if="item.name == '优惠券'" :info="item" />
        <wenzhangCom v-if="item.name == '文章'" :info="item" />
        <spikeCom v-if="item.name == '秒杀'" :info="item" />
        <spikeCom v-if="item.name == '限时折扣'" :info="item" />
        <NyNjCom v-if="item.name == 'N元N件'" :info="item" />
        <NyNjCom v-if="item.name == '满减活动'" :info="item" />
        <goodsCom
          v-if="item.name == '商品'"
          :info="item"
          :onReachBottomChange="item.ispage == 1 ? onReachBottomChange : ''"
        />
      </view>
    </block>

    <u-popup
      mode="center"
      :safeAreaInsetBottom="false"
      :show="popupShow"
      :overlayStyle="{ 'touch-action': 'none' }"
      :customStyle="{ width: '100%' }"
      bgColor="transparent"
    >
      <image
        @click="seeMyCoupon"
        :src="configInfo.newcomer"
        style="width: 100%"
        mode="widthFix"
      />
      <view class="close" @click.stop="closePop"></view>
    </u-popup>

    <u-popup
      mode="center"
      :safeAreaInsetBottom="false"
      :show="popupShow2"
      :overlayStyle="{ 'touch-action': 'none' }"
      :customStyle="{ width: '100%' }"
      bgColor="transparent"
    >
      <image
        @click="seeMyCoupon"
        :src="configInfo.birthday"
        style="width: 100%"
        mode="widthFix"
      />
      <view class="close" @click.stop="popupShow2 = false"></view>
    </u-popup>

    <custom-tab-bar :path="'/pages/index/index'" :list="[]"></custom-tab-bar>
  </view>
</template>

<script>
import customTabBar from "@/components/CustomTabBar";
import navTitleCom from "./components/navTitle.vue";
import searchCom from "./components/search.vue";
import imgCom from "./components/img.vue";
import bannerCon from "./components/banner.vue";
import imgTextNavCom from "./components/imgTextNav.vue";
import videoCom from "./components/video.vue";
import tabsCom from "./components/tabs.vue";
import goodsCateCom from "./components/goodsCate.vue";
import titleCom from "./components/title.vue";
import wangeditorCom from "./components/wangeditor.vue";
import textCom from "./components/text.vue";
import borderCom from "./components/border.vue";
import sellerCom from "./components/seller.vue";
import bulletinCom from "./components/bulletin.vue";
import couponCom from "./components/coupon.vue";
import wenzhangCom from "./components/wenzhang.vue";
import spikeCom from "./components/spike.vue";
import NyNjCom from "./components/NyNj.vue";
import goodsCom from "./components/goods.vue";
import { getToken, setStorage } from "@/utils/storage.js";
import { getEnv } from "@/utils/env.js";
import {
  adornInfo,
  updateParent,
  couponNewcomer,
  couponBirthday,
  config,
} from "@/servers/servers.js";
import { HTTP_STATUS } from "@/servers/config.js";
import { handleLoopCallBack } from "../../utils/util";
import { getGloBalData, getStorage } from "../../utils/storage";
import { go } from "../../utils/wxAPI";
import {
  getDatesForNextDays,
  getDatesFromDayPlusDays,
  isTimeRangePassed,
  addOneHourToTime,
  addOneMinuteToTime,
  getCurrentAndNextHourTimes,
  canBeScheduled,
} from "@/utils/distribution";
export default {
  components: {
    navTitleCom,
    searchCom,
    imgCom,
    bannerCon,
    imgTextNavCom,
    videoCom,
    tabsCom,
    customTabBar,
    goodsCateCom,
    titleCom,
    wangeditorCom,
    textCom,
    borderCom,
    sellerCom,
    bulletinCom,
    couponCom,
    wenzhangCom,
    spikeCom,
    NyNjCom,
    goodsCom,
  },
  data() {
    return {
      popupShow2: false,
      popupShow: false,
      loadingPage: false,
      data: [{}],
      options: {},
      onReachBottomChange: 1,
      configInfo: {},
      menuInfo: {},
    };
  },
  async onLoad(options) {
    this.options = options;
    console.log("options");
    console.log(options);
    uni.hideTabBar();

    //获取当天+3天的时间数组
    console.log(getDatesForNextDays(4));

    //获取延后时间+3天的时间数组
    console.log(getDatesFromDayPlusDays(1, 3));

    //判断时间段是否超过今天的时间段
    console.log(isTimeRangePassed("20:00-20:32"));

    //小时增加
    console.log(addOneHourToTime("2025-01-23 23:45", 2));

    //分钟增加
    console.log(addOneMinuteToTime("2025-01-23 23:59", 1));

    //当前时间延后1个小时的开始时间和结束时间
    console.log(getCurrentAndNextHourTimes());

    //检查给定的时间段是否可以预约
    console.log(canBeScheduled("18:43-20:00"), 1);

    // #ifdef MP-WEIXIN
    this.menuInfo = getApp().globalData.menuInfo;
    // #endif

    if (this.options.uid) {
      this.options.scene = this.options.uid;
    }
    if (getToken()) {
      await this.config();
      if (getStorage("customData")) {
        setTimeout(() => {
          this.data = getStorage("customData");
          this.loadingPage = true;
        }, 1000);
      }
      if (this.options.scene) {
        updateParent({
          id: this.options.scene,
        });
      }
      this.handleCouponNewcomer();
    } else {
      handleLoopCallBack(async () => {
        await this.config();
        if (getStorage("customData")) {
          this.data = getStorage("customData");
          setTimeout(() => {
            this.data = getStorage("customData");
            this.loadingPage = true;
          }, 1000);
        }
        if (this.options.scene) {
          updateParent({
            id: this.options.scene,
          });
        }
        this.handleCouponNewcomer();
      });

      // #ifdef H5
      if (getEnv() == "H5") {
        uni.reLaunch({
          url: "/pages/login/login",
        });
      }
      // #endif
    }
  },
  methods: {
    async config() {
      let birthday = await config({
        key: "birthday.coupon",
      });
      if (birthday.data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        this.configInfo.birthday = JSON.parse(birthday.data.data).img;
      }
      let newcomer = await config({
        key: "newcomer.coupon",
      });
      if (newcomer.data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        this.configInfo.newcomer = JSON.parse(newcomer.data.data).img;
      }
    },
    seeMyCoupon() {
      this.popupShow = false;
      go(`/pages/coupon/myCoupon`);
    },
    async handleCouponNewcomer() {
      let { data } = await couponNewcomer();
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let list = data.data;
        if (list.length !== 0) {
          setTimeout(() => {
            this.popupShow = true;
          }, 1000);
        } else {
          this.handleCouponBirthday();
        }
        // console.log(info);
      }
    },
    async handleCouponBirthday() {
      let { data } = await couponBirthday();
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let list = data.data;
        if (list.length !== 0) {
          this.popupShow2 = true;
        }
        // console.log(info);
      }
    },
    closePop() {
      this.popupShow = false;
      this.popupShow2 = false;
      this.handleCouponBirthday();
    },
  },
  onShareAppMessage: function (e) {
    return {
      title: "乐皇阁",
      path: `/pages/index/index?uid=${getGloBalData("userinfo").id}`,
    };
  },
  onReachBottom() {
    this.onReachBottomChange += 1;
  },
};
</script>
<style>
/* #ifdef MP-WEIXIN */
.content {
  min-height: 100vh;
}

/* #endif */
</style>
<style scoped lang="scss">
.content {
  min-height: 100vh;
}
.close {
  position: absolute;
  width: 100rpx;
  height: 100rpx;
  left: 50%;
  bottom: 0;
  margin-left: -50rpx;
  z-index: 1;
}
</style>
